package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service(interfaceClass = PermissionService.class)
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    /**
     * 分页查询权限数据
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page<Permission> page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        permissionDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增权限信息
     * @param permission
     */
    public void savePermission(Permission permission) {
        //添加日期字符串
        try {
            permission.setCreateTime(DateUtils.parseDate2String(new Date()));
            permissionDao.savePermission(permission);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 根据id删除权限信息
     * @param id
     */
    public void deletePermission(Integer id) {
        permissionDao.deletePermission(id);
    }


    /**
     * 更新权限信息
     * @param permission
     */
    public void updatePermission(Permission permission) {
        permissionDao.updatePermission(permission);
    }

    @Override
    public List<Permission> findAllRole() {

        return permissionDao.findAllRole();
    }
}
